import { createRouter, createWebHistory } from "vue-router";

import { useUserInfo } from "../stores/userInfo";

const staticRoutes = [
  {
    // 首页
    path: "/",
    name: "/",
    component: () => import("@/views/login.vue"),
    meta: {
      title: "KM",
    },
  },
  {
    // 首页
    path: "/dashboard",
    name: "/dashboard",
    component: () => import("@/views/index.vue"),
    meta: {
      title: "KM",
    },
  },
  {
    path: "/home",
    redirect: "/qqCallback",
  },
  {
    // 回调
    path: "/qqCallback",
    name: "/qqCallback",
    component: () => import("@/views/oauth/qqCallback.vue"),
  },
  {
    // 产品页
    path: "/product",
    name: "/product",
    component: () => import("@/views/product/index.vue"),
  },
  {
    // 详情
    path: "/detail",
    name: "/product-detail",
    alias: "/product-detail.html",
    component: () => import("@/views/detail/index.vue"),
  },
  {
    // 充值页面
    path: "/recharge",
    name: "/recharge",
    alias: "/recharge.html",
    component: () => import("@/views/recharge/index.vue"),
  },
  {
    // 订阅
    path: "/subscribe",
    name: "/subscribe",
    alias: "/subscribe.html",
    component: () => import("@/views/subscribe/index.vue"),
  },
  {
    // 规则协议
    path: "/agreement",
    name: "/agreement",
    alias: "/agreement.html",
    component: () => import("@/views/agreement/index.vue"),
  },
  {
    // 充值页面
    path: "/help_center",
    name: "/help_center",
    alias: "/help_center.html",
    component: () => import("@/views/personal/help_center.vue"),
  },
  {
    path: "/:path(.*)*",
    redirect: "/404",
  },
  {
    // 404
    path: "/404",
    name: "notFound",
    component: () => import("@/views/common/error/404.vue"),
  },
  {
    // 登录页
    path: "/login",
    name: "userLogin",
    component: () => import("@/views/login.vue"),
    meta: {
      title: "用户登录",
    },
  },
  {
    path: "/user",
    name: "user",
    component: () => import("@/views/user/index.vue"),
    redirect: "/userhome",
    children: [
      {
        // 个人中心
        path: "/userhome",
        name: "userHome",
        component: () => import("@/views/user/home.vue"),
        meta: {
          title: "首页",
        },
      },
      {
        // 个人中心
        path: "/account_sys",
        name: "account_sys",
        component: () => import("@/views/user/pwd.vue"),
        meta: {
          title: "账号密码",
        },
      },
      {
        // 个人中心
        path: "/user_balance",
        name: "user_balance",
        component: () => import("@/views/user/balance.vue"),
        meta: {
          title: "账号余额",
        },
      },
      {
        // 个人中心
        path: "/recharge_order",
        name: "recharge_order",
        component: () => import("@/views/user/recharge.vue"),
        meta: {
          title: "订单充值",
        },
      },
      {
        // 个人中心
        path: "/user_dowload",
        name: "user_dowload",
        component: () => import("@/views/user/dowload.vue"),
        meta: {
          title: "我的下载",
        },
      },
      {
        // 个人中心
        path: "/user_collect",
        name: "user_collect",
        component: () => import("@/views/user/collect.vue"),
        meta: {
          title: "我的收藏",
        },
      },
      {
        // 个人中心
        path: "/user_footprint",
        name: "user_footprint",
        component: () => import("@/views/user/footprint.vue"),
        meta: {
          title: "我的足迹",
        },
      },
      {
        // 个人中心
        path: "/user_message",
        name: "user_message",
        component: () => import("@/views/user/message.vue"),
        meta: {
          title: "消息中心",
        },
      },
    ],
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes: staticRoutes,
});
// 添加全局前置守卫
router.beforeEach(async (to, from, next) => {
    const userInfo = useUserInfo();
    if (to.path === '/' || to.path === '/login') {
        if(to.path === '/' && userInfo.token){
            next({
                path: '/dashboard',
              });
        }
        next(); // 或者根据需求重定向到其他地方，如直接显示登录页 next('/login');
      } else if (!userInfo.token) {
        // 用户未登录，重定向到登录页，并附带重定向后的目标URL
        next({
          path: '/login',
        });
      } else {
        if (!userInfo.token) {
          // 用户未登录，重定向到登录页，并附带重定向后的目标URL
          next({
            path: '/login',
          });
        }else{
          next(); // 确保调用 next()
        }
    

      }
  });
export default router;
